package org.springblade.modules.admin.task;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springblade.modules.admin.entity.EstateElevator;
import org.springblade.modules.admin.enums.EffectiveStatusEnum;
import org.springblade.modules.admin.service.IEstateElevatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/6/6 14:14
 */
@Component
public class EstateApplicationElevatorJob {

	@Autowired
	private IEstateElevatorService estateElevatorService;


	/**
	 * 定时更新维保的生效状态和梯联网的生效状态
	 * 数据量增长比较慢,前期获取所有更新即可
	 */
	//@Scheduled(cron = "0 0/10 * * * ?")
	@XxlJob(value = "updateEstateElevator")
	public void updateEstateElevator() {
		XxlJobHelper.handleSuccess("梯无忧梯联网状态更新-(updateEstateElevator)-[执行]开始");
		try {
			List<EstateElevator> elevatorList = estateElevatorService.list();
			List<EstateElevator> updateList = new ArrayList<>(50);
			for (EstateElevator estateElevator : elevatorList) {
				Integer maintainStatus = estateElevator.getMaintainStatus();
				Integer tlwStatus = estateElevator.getTlwStatus();
				Integer nowDate = Convert.toInt(DateUtil.format(new Date(), "yyyyMMdd"));
				if (estateElevator.getMaintainStartTime() != null) {
					Integer maintainStartTime = Convert.toInt(DateUtil.format(estateElevator.getMaintainStartTime(), "yyyyMMdd"));
					Integer maintainEndTime = Convert.toInt(DateUtil.format(estateElevator.getMaintainEndTime(), "yyyyMMdd"));
					if (nowDate < maintainStartTime) {
						estateElevator.setMaintainStatus(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
					} else if (nowDate >= maintainStartTime && nowDate <= maintainEndTime) {
						estateElevator.setMaintainStatus(EffectiveStatusEnum.EFFECTED.getStatus());
					} else if (nowDate >= maintainEndTime) {
						estateElevator.setMaintainStatus(EffectiveStatusEnum.LOSE_EFFICACY.getStatus());
					} else {
						estateElevator.setMaintainStatus(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
					}
				} else {
					estateElevator.setMaintainStatus(EffectiveStatusEnum.NO_OPEN.getStatus());
				}

				if (estateElevator.getTlwStartTime() != null) {
					Integer tlwStartTime = Convert.toInt(DateUtil.format(estateElevator.getTlwStartTime(), "yyyyMMdd"));
					Integer tlwEndTime = Convert.toInt(DateUtil.format(estateElevator.getTlwEndTime(), "yyyyMMdd"));
					if (nowDate < tlwStartTime) {
						estateElevator.setTlwStatus(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
					} else if (nowDate >= tlwStartTime && nowDate <= tlwEndTime) {
						estateElevator.setTlwStatus(EffectiveStatusEnum.EFFECTED.getStatus());
					} else if (nowDate >= tlwEndTime) {
						estateElevator.setTlwStatus(EffectiveStatusEnum.LOSE_EFFICACY.getStatus());
					} else {
						estateElevator.setTlwStatus(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
					}
				} else {
					estateElevator.setTlwStatus(EffectiveStatusEnum.NO_OPEN.getStatus());
				}

				//如果状态跟原有的状态不一致表示状态发生了变化，需要个更新数据
				if (!maintainStatus.equals(estateElevator.getMaintainStatus()) || !tlwStatus.equals(estateElevator.getTlwStatus())) {
					updateList.add(estateElevator);
				}
			}
			if (updateList.size() > 0) {
				estateElevatorService.updateBatchById(elevatorList);
			}
			XxlJobHelper.handleSuccess("梯无忧梯联网状态更新-(updateEstateElevator)-[执行]成功");
		} catch (Exception e) {
			XxlJobHelper.handleSuccess("梯无忧梯联网状态更新-(updateEstateElevator)-[执行]失败" + e);

		}

	}
}
